package com.haven.webrobotserver.endRunner;

import com.haven.webrobotserver.domain.EndRunner;

import java.util.*;

public class EndRunnerStatic {
    private static Set<EndRunner> ONLINE_ENDRUNNERS=new HashSet<>();

    static{
        new CheckOffLineEndRunnerThread().start();
    }

    @SuppressWarnings("InfiniteLoopStatement")
    private static class CheckOffLineEndRunnerThread extends Thread{
        @Override
        public void run() {
            super.run();

            while(true){
                long currentTimeStamp = System.currentTimeMillis();

                for(Iterator<EndRunner> iterator = ONLINE_ENDRUNNERS.iterator();iterator.hasNext();){

                    EndRunner next = iterator.next();
                    long timeNoUpdate = currentTimeStamp - next.getLastHeartBeatTime();

                    if(timeNoUpdate > 10000){
                        iterator.remove();
                    }

                }

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


        }
    }

    public static Set<EndRunner> getOnlineEndRunners(){
        return ONLINE_ENDRUNNERS;
    }

}
